package com.aiyige.utils.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.aiyige.R;

public class LoadingFrame extends FrameLayout {
    int contentLayoutResId = R.layout.wild_card_item;
    View contentView;
    float alphaDelta = 0f;

    public LoadingFrame(@NonNull Context context) {
        super(context);
        init(null);
    }

    public LoadingFrame(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public LoadingFrame(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.LoadingFrame);
            contentLayoutResId = a.getResourceId(R.styleable.LoadingFrame_lf_contentview, R.layout.wild_card_item);
            a.recycle();
        }
        contentView = LayoutInflater.from(getContext()).inflate(contentLayoutResId, this, true);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        autoConfigAlphaDelta();
        contentView.setAlpha(contentView.getAlpha() + alphaDelta);
        postInvalidate();
    }

    private void autoConfigAlphaDelta() {
        if (contentView.getAlpha() >= 1.0f) {
            alphaDelta = -0.03f;
        } else if (contentView.getAlpha() < 0.3f) {
            alphaDelta = 0.03f;
        }
    }


}
